package com.yjn.blog.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.yjn.blog.dao.UserModelDao;
import com.yjn.blog.model.UserModel;
import com.yjn.blog.model.http.UserReq;
import com.yjn.blog.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;

/**
 * Classname: UserServiceImpl
 * Package: com.yjn.blog.service.impl
 * Description:
 *
 * @Author 余集楠
 * @Create 2024/7/16 0016 20:13
 * @Version 1.8
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserModelDao userModelDao;

    //注册
    @Override
    public Boolean regist(String username,String password) {
        String md5Password=SecureUtil.md5(password);

        return userModelDao.regist(username,md5Password);
    }

    //登录
    @Override
    public UserModel login( UserReq req) throws Exception {
        //根据username查询user
        UserModel user=userModelDao.selectByUsername(req.getUsername());

        //判断是否为空，如果为空，抛出异常
        if(user == null){
            throw new Exception("用户名错误");
        }

        //对用户输入的密码进行加密
        String password= SecureUtil.md5(req.getPassword());
        //比较输入的密码和数据库密码是否一致，不一致，则抛出异常
        if(!password.equals(req.getPassword())){
            throw new Exception("密码输入错误");
        }
        return user;
    }

    @Override
    public UserModel userInfo(String username) {
        //根据username获取整条用户信息
        UserModel user=userModelDao.selectByUserInfo(username);
        return user;
    }
}
